package net.kaoslabs;

import java.io.IOException;

import net.milkbowl.vault.permission.Permission;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import net.kaoslabs.extras.Metrics;

public class CustomJoinMessage extends JavaPlugin {
	
	public static String name;
	public static String version;
	public static CustomJoinMessage plugin;
	public final PlayerListener pl = new PlayerListener(this);
	public Permission permission = null;
	
	@Override
	public void onEnable() {
		PluginManager pm = getServer().getPluginManager();
		
		if (true) { // modificare il controllo
			getLogger().info("Generating Config...");
			getConfig().options().header("Use %u to use the user's username");
			getConfig().addDefault("use-default", true);
			getConfig().addDefault("use-groups", false);
			
			getConfig().addDefault("default.join-message", "%u joined the game.");
			getConfig().addDefault("default.leave-message", "%u left the game.");
			getConfig().addDefault("default.kick-message", "%u left the game.");
			
			getConfig().addDefault("users.Kaosvf.join-message", "&3%u il POTENTE joined the game.");
			getConfig().addDefault("users.Kaosvf.leave-message", "&3%u il POTENTE left the game.");
			getConfig().addDefault("users.Kaosvf.kick-message", "&3%u the awesome was kicked.");
			
			getConfig().addDefault("groups.Admins.join-message", "%u joined the game.");
			getConfig().addDefault("groups.Admins.join-message", "%u joined the game.");
			getConfig().addDefault("groups.Admins.leave-message", "%u left the game.");
			getConfig().addDefault("groups.Admins.kick-message", "%u left the game.");
			getConfig().options().copyDefaults(true);
			saveConfig();
		}
		if (getConfig().getBoolean("use-groups")) {
			if (pm.getPlugin("Vault") != null) {
				if (setupPermissions()) {
					getLogger().info("Using: "+permission.getName()+" for permissions!");
				} else {
					getLogger().info("Permission plugin not found... shutting down!");
					pm.disablePlugin(this);
				}
			} else {
				getLogger().info("Permission plugin not found... shutting down!");
				pm.disablePlugin(this);
			}
		}
		pm.registerEvents(this.pl, this);
		try {
		    Metrics metrics = new Metrics(this);
		    getLogger().info("Enabling Metrics...");
		    metrics.start();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	}
	
	@Override
	public void onDisable() {
		
	}
	
	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
		name = getDescription().getName();
		version = getDescription().getVersion();
		
		if (commandLabel.contains("cjm")) {
			if (args.length == 0) {
				sender.sendMessage("["+ChatColor.GREEN+name+ChatColor.WHITE+"] version ["+ChatColor.RED+version+ChatColor.WHITE+"]");
			} else if (args[0].equalsIgnoreCase("reload")) {
				if (sender instanceof Player) { // Command used by a player
					if (sender.hasPermission("cjm.reload")) {
						reloadConfig();
						sender.sendMessage(ChatColor.GREEN + "CustomJoinMessage Configuration reloaded!");
					} else {
						sender.sendMessage(ChatColor.RED + "You do not have permission.");
					}
				} else { // Command used from Console
					reloadConfig();
					sender.sendMessage("CustomJoinMessage Configuration reloaded!");
				}
			}
		}
		return false;
	}
	
    private boolean setupPermissions() {
    	RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
    	if (permissionProvider != null) {
    		permission = permissionProvider.getProvider();
    		}
    	return (permission != null);
    }
}
